import 'package:flutter_riverpod/flutter_riverpod.dart';

/// 页面状态枚举
enum PageState {
  idle, // 闲置状态
  loading, // 加载中状态
  success, // 加载成功状态
  empty, // 空数据状态
  error, // 错误状态
}

/// 基础状态数据类
class BaseState<T> {
  /// 页面状态
  final PageState pageState;

  /// 数据内容
  final T? data;

  /// 错误信息
  final String? errorMessage;

  /// 异常信息
  final Exception? exception;

  const BaseState({
    this.pageState = PageState.idle,
    this.data,
    this.errorMessage,
    this.exception,
  });

  // 是否闲置
  bool get isIdle => pageState == PageState.idle;

  /// 是否正在加载
  bool get isLoading => pageState == PageState.loading;

  /// 是否成功
  bool get isSuccess => pageState == PageState.success;

  /// 是否为空
  bool get isEmpty => pageState == PageState.empty;

  /// 是否有数据
  bool get hasData => data != null;

  /// 是否为错误状态
  bool get isError => pageState == PageState.error;

  /// 复制状态
  BaseState<T> copyWith({
    PageState? pageState,
    T? data,
    String? errorMessage,
    Exception? exception,
    bool clearErrorMessage = false,
    bool clearData = false,
  }) {
    return BaseState<T>(
      pageState: pageState ?? this.pageState,
      data: clearData ? null : (data ?? this.data),
      errorMessage:
          clearErrorMessage ? null : (errorMessage ?? this.errorMessage),
      exception: exception ?? this.exception,
    );
  }

  @override
  String toString() {
    return 'BaseState(pageState: $pageState, data: $data, errorMessage: $errorMessage)';
  }
}

